﻿using CaltechSoft.Service.DBCommand;
using CaltechSoft.Service.EntityFW.Context;
using CaltechSoft.Service.EntityFW.Entity;
using CaltechSoft.Service.EntityFW.Repository;
using CaltechSoft.Service.Infrastructure.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaltechSoft.Service.CommandService
{
    public class CertificateStandardCommandService : ICertificateStandardCommandService
    {
        private readonly IUnitOfWork _unitOfWork;

        public CertificateStandardCommandService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public int CreateCertificateStandard(CreateCertificateStandardCommand command)
        {
            Guard.ArgumentIsNotNull(command, "command");
            Guard.Against<ArgumentException>(string.IsNullOrEmpty(command.Manufacturer), "Standard manufacturer can not be empty");

            using (var context = new CaltechContext())
            {
                var existingItem = context.Set<CertificateStandard>().FirstOrDefault(n => n.Manufacturer == command.Manufacturer &&
                                                                                     n.ModelNumber == command.ModelNumber &&
                                                                                     n.TestReportNumber == command.TestReportNumber &&
                                                                                     n.StandardNo == command.StandardNo
                                                                                    );
                if (existingItem != null)
                {
                    return existingItem.CertificateStandardId;
                }
                var cert = new CertificateStandard
                {
                    Manufacturer = command.Manufacturer,
                    DueDate = command.DueDate,
                    ModelNumber = command.ModelNumber,
                    StandardNo = command.StandardNo,
                    SerialNumber = command.SerialNumber,
                    TestReportNumber = command.TestReportNumber,
                    TechnicianName = command.TechnicianName,
                    Description = "",
                    CreatedBy = command.CreatedBy,
                    CreatedDate = command.CreatedDate,
                    UpdatedBy = command.UpdatedBy,
                    UpdatedDate = command.UpdatedDate

                };
                context.Set<CertificateStandard>().Add(cert);
                context.SaveChanges();
                return cert.CertificateStandardId;
            }
        }

        public int AddStandard(CreateCertificateStandardCommand command)
        {
            Guard.ArgumentIsNotNull(command, "command");
            Guard.Against<ArgumentException>(string.IsNullOrEmpty(command.Manufacturer), "Standard manufacturer can not be empty");

            using (var context = new CaltechContext())
            {
                var existingItem = context.Set<CertificateStandard>().FirstOrDefault(n => n.StandardNo == command.StandardNo);
                Guard.Against<ArgumentException>(existingItem != null, "This standard no have been already existed !");
                var cert = new CertificateStandard
                {
                    Manufacturer = command.Manufacturer,
                    DueDate = command.DueDate,
                    ModelNumber = command.ModelNumber,
                    StandardNo = command.StandardNo,
                    SerialNumber = command.SerialNumber,
                    TestReportNumber = command.TestReportNumber,
                    TechnicianName = command.TechnicianName,
                    Description = command.Description,
                    CreatedBy = command.CreatedBy,
                    CreatedDate = command.CreatedDate,
                    UpdatedBy = command.UpdatedBy,
                    UpdatedDate = command.UpdatedDate
                };
                context.Set<CertificateStandard>().Add(cert);
                context.SaveChanges();
                return cert.CertificateStandardId;
            }
        }

        public int UpdateCertificateStandard(CreateCertificateStandardCommand command)
        {
            using (var context = new CaltechContext())
            {
                var existingItem = context.Set<CertificateStandard>().FirstOrDefault(n => n.Manufacturer == command.Manufacturer && n.StandardNo == command.StandardNo && n.CertificateStandardId != command.CertificateStandardId.Value);
                Guard.Against<ArgumentException>(existingItem != null, "This standard no have been already existed !");

                CertificateStandard cert = new CertificateStandard
                {
                    CertificateStandardId = command.CertificateStandardId.Value,
                    Manufacturer = command.Manufacturer,
                    DueDate = command.DueDate,
                    ModelNumber = command.ModelNumber,
                    StandardNo = command.StandardNo,
                    SerialNumber = command.SerialNumber,
                    TestReportNumber = command.TestReportNumber,
                    TechnicianName = command.TechnicianName,
                    Description = command.Description,
                    CreatedBy = command.CreatedBy,
                    CreatedDate = command.CreatedDate,
                    UpdatedBy = command.UpdatedBy,
                    UpdatedDate = command.UpdatedDate
                };

                _unitOfWork.Repository<CertificateStandard>().Update(cert);
                _unitOfWork.Save();
                return cert.CertificateStandardId;
            }
        }

        public void Delete(int standardId)
        {
            var isCertificateDetails = _unitOfWork.Repository<CertificateDetails>().Get(n => n.CertificateStandardId == standardId).Any();
            Guard.Against<ArgumentException>(isCertificateDetails, "There are many certificate use this standard. Can not delete this standard!");

            _unitOfWork.Repository<CertificateStandard>().Delete(standardId);
            _unitOfWork.Save();
        }
    }
}
